package cn.bdqn.common;

import cn.bdqn.common.error.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

//全局异常处理器
@ControllerAdvice
public class ExceptionHandler {

    // 在@ExceptionHandler注解中指定异常类型
    @org.springframework.web.bind.annotation.ExceptionHandler
    @ResponseBody
    public Result exceptionMapping(Exception exception) {// 方法形参位置接收SpringMVC捕获到的异常对象
        if(exception instanceof MyException){
            MyException e= (MyException) exception;
            return Result.fail(e.getCode(),e.getMessage(),e.getMessage());
        }else {
            return Result.fail(700,exception.getMessage(),exception.getMessage());
        }
    }
}